package interfaz;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;

public class PanelVerCorreo extends JPanel
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JTextPane textVisCorreo;
	
	private JTextField textRemitente;
	
	private JTextField textAsunto;
	
	private JTextField textFecha;
	
	private JButton reenviar;
	
	private JButton responder;
	
	private InterfazCorreo principal;
	
	public PanelVerCorreo(InterfazCorreo ic)
	{
		principal = ic;
		
		setLayout(new BorderLayout());
		
		//Area
		setBorder(new TitledBorder("Correo"));
		
		JScrollPane scroll = new JScrollPane();
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		textVisCorreo = new JTextPane();
		textVisCorreo.setEditable(false);
		scroll.getViewport().add(textVisCorreo);
		add(scroll, BorderLayout.CENTER);
		
		//Info
		JPanel panelInfo = new JPanel();
		GridBagLayout layout = new GridBagLayout();
		panelInfo.setLayout(layout);
		
		GridBagConstraints gbc = new GridBagConstraints();
		
		JLabel labRemitente = new JLabel("Remitente: ");
		gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(5, 5, 0, 0);
        layout.setConstraints(labRemitente, gbc);
		panelInfo.add(labRemitente);
		
		gbc = new GridBagConstraints();
		textRemitente = new JTextField(37);
		textRemitente.setEditable(false);
		gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.insets = new Insets(5, 5, 0, 5);
        gbc.weightx = 1.0D;
        layout.setConstraints(textRemitente, gbc);
		panelInfo.add(textRemitente);
		
		gbc = new GridBagConstraints();
		JLabel labAsunto = new JLabel("Asunto: ");
		gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(5, 5, 0, 0);
        layout.setConstraints(labAsunto, gbc);
		panelInfo.add(labAsunto);
		
		gbc = new GridBagConstraints();
		textAsunto = new JTextField(37);
		textAsunto.setEditable(false);
		gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.insets = new Insets(5, 5, 0, 5);
        gbc.weightx = 1.0D;
        layout.setConstraints(textAsunto, gbc);
		panelInfo.add(textAsunto);
		
		gbc = new GridBagConstraints();
		JLabel labFecha = new JLabel("Fecha: ");
		gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(5, 5, 0, 0);
        layout.setConstraints(labFecha, gbc);
		panelInfo.add(labFecha);
		
		gbc = new GridBagConstraints();
		textFecha = new JTextField(37);
		textFecha.setEditable(false);
		gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.insets = new Insets(5, 5, 0, 5);
        gbc.weightx = 1.0D;
        layout.setConstraints(textFecha, gbc);
		panelInfo.add(textFecha);
		
		add(panelInfo, BorderLayout.NORTH);
		
		JPanel panelBotones = new JPanel();
		responder = new JButton("Responder");
		responder.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					principal.responderMensaje();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		responder.setEnabled(false);
		panelBotones.add(responder);
		
		reenviar = new JButton("Reenviar");
		reenviar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					principal.reenviarMensaje();
				} catch (IllegalStateException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		reenviar.setEnabled(false);
		panelBotones.add(reenviar);		
		
		add(panelBotones, BorderLayout.SOUTH);
	}
	
	public void setText(String text)
	{
		textVisCorreo.setText(text);
	}

	public void setTextRemitente(String text) {
		textRemitente.setText(text);
	}

	public void setTextAsunto(String text) {
		textAsunto.setText(text);
	}

	public void setTextFecha(Date fecha) {
		Format formato = new SimpleDateFormat("E, dd MMM yyyy HH:mm");
		String fechaFomat = formato.format(fecha);
		textFecha.setText(fechaFomat);
	}

	public JTextPane getArea() {
		return textVisCorreo;
	}

	public JButton getReenviar() {
		return reenviar;
	}

	public JButton getResponder() {
		return responder;
	}
	
	
	
	
	
		
	
}
